#include "priority_queue.h"
#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <cassert>

class Solution {
public:
    struct cmp {  // 注意这里的结构体定义要完整
        bool operator()(const std::pair<int, int>& a, const std::pair<int, int>& b) {
            return a.second < b.second;
        }
    };

    std::vector<int> topKFrequent(std::vector<int>& nums, int k) {
        std::unordered_map<int, int> occurrences;
        for (auto& v : nums) {
            occurrences[v]++;
        }

        // 使用结构体作为比较函数
        Priority_Queue<std::pair<int, int>, std::vector<std::pair<int, int>>, cmp> q;
        for (auto& [nums, num] : occurrences) {
            std::cout<<"push: "<<nums<<" "<<num<<std::endl;
            if (q.size() == k) {
                if (q.top().second < num) {
                    q.pop();
                    q.push(std::make_pair(nums,num));
                }
            } else {
                q.push(std::make_pair(nums, num));
            }
        }
        std::vector<int> ret;
        while (!q.empty()) {
            ret.emplace_back(q.top().first);
            q.pop();
        }
        return ret;
    }
};

int main() {
    Solution solution;
    std::vector<int> nums = {1,1,2,3,4,1,1,4,5,1,3,6,7,7,2,4};
    int k = 2;
    std::vector<int> result = solution.topKFrequent(nums, k);
    for (int num : result) {
        std::cout << num << " ";
    }
    std::cout << std::endl;
    return 0;
}